import Common from '../common.js'
import utils from '../utils'

// Test if the second endpoint of an arc is the endpoint of any path in any layer
function getPathEndpointTest(layers, arcs) {
  const index = new Uint8Array(arcs.size())
  layers.forEach(function(lyr) {
    if (Common.layerHasPaths(lyr)) {
      lyr.shapes.forEach(addShape)
    }
  })

  function addShape(shape) {
    utils.forEachShapePart(shape, addPath)
  }

  function addPath(path) {
    addEndpoint(~path[0])
    addEndpoint(path[path.length - 1])
  }

  function addEndpoint(arcId) {
    const absId = Common.absArcId(arcId)
    const fwd = absId == arcId
    index[absId] |= fwd ? 1 : 2
  }

  return function(arcId) {
    const absId = Common.absArcId(arcId)
    const fwd = absId == arcId
    const code = index[absId]
    return fwd ? (code & 1) == 1 : (code & 2) == 2
  }
}
export default getPathEndpointTest
